package com.scau.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.scau.dao.AdminDao;
import com.scau.pojo.Admin;
import com.scau.service.AdminService;


@RestController
@RequestMapping("/adminController")
public class AdminController{
	
	@Autowired
	AdminService adminService;
	
	@Autowired
	AdminDao adminDao;
	
	
	@RequestMapping("/queryAdmin")
	public List<Admin> queryAdmin() {
		List<Admin> result = adminService.getAllAdmin();
		return result;
	}
	
	@RequestMapping("/editAdmin")
	public int edit(@RequestBody JSONObject jsonObject) throws Exception{
		int result=0;
		try{
			result=adminService.updateAdmin(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/checkAdminPwd")
    public Map<String,Object> checkAdminPwd(@RequestParam String name,@RequestParam String password) throws Exception{
		Map<String,Object> resultMap=new HashMap<>();
    	Map<String,Object> map=adminDao.getAdmin(name);
    	Admin admin=(Admin)map.get("admin");
    	String oldPwd=admin.getPassword();
    	if(oldPwd.equals(password)){
    		resultMap.put("returnCode", 1);
    	}else{
    		resultMap.put("returnCode", 0);
    	}
		return resultMap;
		
	}
	
	@RequestMapping("/getAdminInfo")
    public Admin getAdminInfo(@RequestParam String name) throws Exception{
		Admin admin=new Admin();
		try {
			Map<String,Object> map=adminDao.getAdmin(name);
	    	admin=(Admin)map.get("admin");
	    	return admin;
		} catch (Exception e) {
			return null;
		}
    	
		
		
	}

}
